{% extends 'layout/manage.html' %}
{% load static %}

{% block css %}
    <link rel="stylesheet" href="{% static 'plugins/editor-md/editor.md-master/css/editormd.min.css' %}">
    <style>
        .panel-body {
            padding: 0;
        }

        .panel-default .panel-heading {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
        }

        .title-list {
            border-right: 1px solid #dddddd;
            min-height: 500px;
        }

        .content {
            border-left: 1px solid #dddddd;
            min-height: 600px;
            margin-left: -1px;
        }
    </style>
{% endblock %}

{% block title %}任务交流{% endblock %}
{% block content %}
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading">
                <div>
                    <i class="fa fa-comments" aria-hidden="true"/></i>任务交流区
                </div>

            </div>
            <div class="panel-body">
                <div class="col-sm-3 title-list">
                    目录
                </div>

                <div class="col-sm-9 content">
                    <form method="post" novalidate>
                        {% csrf_token %}
                        {% for field in form %}
                            {% if field.name == "content" %}
                            <div class="form-group">
                                <label for="{{ field.id_for_label }}">{{ field.label}}</label>
                                <div id="editor">
                                    {{ field }}
                                </div>
                                <span class="error-msg">{{ field.errors.0 }}</span>
                            </div>
                            {% else %}
                                <div class="form-group">
                                    <label for="{{ field.id_for_label }}">{{ field.label}}</label>
                                    {{ field }}
                                    <span class="error-msg">{{ field.errors.0 }}</span>
                                </div>
                            {% endif %}
                        {% endfor %}

                        <button type="submit" class="btn btn-default">提交</button>
                    </form>
                </div>
            </div>
            <div></div>
        </div>
    </div>

    </div>
{% endblock %}

{% block js %}
    <script src="{% static 'plugins/editor-md/editor.md-master/editormd.min.js' %}"></script>
    <script>
        var WIKI_DETAIL_URL = "http://localhost:8000/manage/{{ request.People.project.id }}/wiki/";
        $(function () {
            initCatalog();
            initEditorMd()
        });
        /*
        * Editormd初始化
        * */
        function initEditorMd() {
            editormd('editor',{
                placeholder: "请输入内容",
                width: "100%",
                height: 600,
                path:"{% static 'plugins/editor-md/editor.md-master/lib/' %}"
            })
        }

        function initCatalog() {
            $.ajax({
                url: '/manage/{{ request.People.project.id }}/wiki/catalog',
                type: 'GET',
                dataType: 'JSON',
                success: function (res) {
                    if (res.status) {
                        $.each(res.data, function (index, item) {
                            var href = WIKI_DETAIL_URL + "?wiki_id=" + item.id;
                            var li = $("<li>").attr("id", "id_" + item.id).append($('<a>').attr('href', href).text(item.title)).append($('<ul>'));
                            if (!item.parent_id) {
                                $("#catalog").append(li);
                            } else {
                                $("#id_" + item.parent_id).children("ul").append(li);
                            }
                        })

                    } else {
                        alert("初始化目录失败")
                    }
                }
            })
        }

    </script>
{% endblock %}